<template>
<div>
  <el-table :data="tableData" stripe style="width: 100%" v-loading="loading">
    <el-table-column header-align="center" prop="name" align="center" label="区域名" width="180"></el-table-column>
    <el-table-column header-align="center" prop="cityName" align="center" label="城市" width="180"></el-table-column>
    <el-table-column header-align="center" prop="cityId" align="center" label="区域Id"></el-table-column>
    <el-table-column header-align="center" align="center" label="操作">
      <template slot-scope="scope">
        <!-- scope.row是本行对应的数据 -->
        <router-link :to="`/area/edit/${scope.row.areaId}`">
          <el-button type="text" size="small">编辑</el-button>
        </router-link>
        <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
   <el-row v-if="value"> 
      <el-pagination
        @current-change="changepage"
        :page-size="pageSize"
        layout="prev, pager, next"
        :total="total"
        class="pagination"
      ></el-pagination>
    </el-row>
</div>
</template>

<script>
export default {
  data() {
    return {
      value:false,
      loading: false,
      tableData: [],
       pageNum: 1,
      pageSize: 10,
      total: 0,
    };
  },
  created() {
    this.getlist(this.pageNum);
  },
  methods: {
    async getlist(pageNum) {
      let url = "http://132.232.87.95:3000/admin/area/getList";
      this.loading = true;
      try {
        let res = await this.axios.post(url,{
           pageSize: this.pageSize,
          pageNum
        });
        this.loading = false;
         this.total = res.data.total;
        this.tableData = res.data.areas;
      } catch (error) {
        this.loading = false;
      }
       //分页少于2页时不显示页码
      if(this.total>this.pageSize){
        this.value=true
      }
    },

     async changepage(pageNum) {
      let dete = await this.getlist([pageNum])
     },
    handleDelete($index, row) {
      let btn = confirm("是否要删除");
      if(btn===true){
      let { cityId } = row;
      let url = "/area/delById";
      let data = { cityId };
      this.axios.post(url);
      let res = this.$axios.post(url, row);
      console.log(res);
      this.tableData.splice($index, 1);
      this.$message({
          message: "删除成功"
        });
      }else{

      }
     
    }
  }
};
</script>